define(function(require, exports){
	var $		= require("jquery");
	var pubjs	= require('pubjs');
	var view	= require('@base/view');
	var util	= require('util');

	var previewAndPreAudit = view.widget.extend({
		init: function( config ) {
			config = pubjs.conf( config, {
				"preAuditUrl": 'creativeAudit/preAudit',
				"getAuditResultUrl": 'creativeAudit/getAuditResult',
				"auditUrl": 'creativeAudit/audit',
				"layout": {
					"module": '@base/dialog.base',
					"mask": 1,
					"title": LANG('创意预审核'),
					"showFoot": true, // 显示底层的取消和完成按钮
					"width": 500,
					"close": null,
					"silence": false // 点击按钮发消息
					,"buttonConfig":{
						// 确定按钮样式
						"ok": {
							"value":LANG("确定")
						}
					}
				},
				'view_model':{
					Name:'',//创意名
					_id:0,
					CreateUserName:''
				}
			});
			this.Super( 'init', arguments );
			//默认模式
			this.$mode = 'preAudit';
		},
		afterBuild:function(){
			var self = this;
			var el = self.getDOM();
			var html = [
				'<div class="P-dspUserCharge pl20">',
					'<div class="M-formConGroup">',
						'<label class="group-label">' + LANG('创意ID：') + '</label>',
						'<div class="group-content">',
							'<span class="M-formInput" ms-text="_id"/>',
						'</div>',
					'</div>',
					'<div class="M-formConGroup">',
						'<label class="group-label">' + LANG('创意名称：') + '</label>',
						'<div class="group-content">',
							'<span class="M-formInput" ms-text="Name"/>',
						'</div>',
					'</div>',
					'<div class="M-formConGroup">',
						'<label class="group-label" style="vertical-align: top">' + LANG('客户名称：') + '</label>',
						'<div class="group-content">',
							'<span class="M-formInput" ms-text="CreateUserName"/>',
						'</div>',
				    '</div>',
					'<div class="M-formConGroup exchange">',
						'<label class="group-label" style="vertical-align: top">' + LANG('渠道：') + '</label>',
						'<div class="group-content">',
							'<div pub-name="exchangeIds" pub-mod="modules/checkBoxGroup.base"/>',
						'</div>',
					'</div>',
					'<div class="M-formConGroup preAudit">',
						'<label class="group-label">' + LANG('预审状态：') + '</label>',
						'<div class="group-content">',
							'<span pub-name="preStatus" pub-mod="@base/common/input.radioGroup"/>',
						'</div>',
					'</div>',
					'<div class="M-formConGroup preAudit">',
						'<label class="group-label">' + LANG('审核理由：') + '</label>',
						'<div class="group-content">',
							'<span class="mr10 M-formInput" pub-name="preInfo" pub-mod="@base/common/input.text"/>',
						'</div>',
					'</div>',
				'</div>'
			].join('');
			$(html).appendTo( el );
			self.buildFromTemplate({
				'exchangeIds':{
					css:{'display':'inline-block'}
				},
				'preStatus': {
					'data':[
						{text:LANG('通过'),value:'1'},
						{text:LANG('拒绝'),value:'-1'}
					]
				},
				'preInfo':{
					width:400
				}
			});
		},
		onData: function( err, data ) {
			if( err ){
				pubjs.alert( err.message );
				return false;
			}
			this.setData( data );
		},
		setReject:function(rejectReason){
			//在setData后调用
			this.$.preStatus.setValue(-1);
			this.$.preInfo.setValue(rejectReason);
			return this;
		},
		setData: function( data ){
			var self = this;
			var vm = self.vm.$;

			vm.CreateUserName = data.CreateUserName;
			vm.Name = data.Name;
			vm._id = data._id;

			if (data.AuditList && data.AuditList.length >0){
				var items = [],audit;
				for (var i = 0; i< data.AuditList.length; i++){
					audit = data.AuditList[i];
					items.push({id:audit.ExchangeId,name:audit.ExchangeName});
				}

				this.$.exchangeIds.setData(items);
				this.$.exchangeIds.show();
				this.find('.exchange').show();
			} else {
				this.$.exchangeIds.setData([]);
				this.$.exchangeIds.hide();
				this.find('.exchange').hide();
			}

			return this;
		},
		getData: function( data ){
			var vm = this.vm.$;
			var exchangeIds = this.$.exchangeIds.getValue();
			exchangeIds = exchangeIds.map(function(id){return +id;});
			var data =  {
				_id: vm._id,
				Name:vm.Name,
				CreateUserName:vm.CreateUserName,
				exchangeIds: exchangeIds
			};

			if (this.$mode == 'preAudit'){
				data.preStatus = +this.$.preStatus.getValue();
				data.preInfo = this.$.preInfo.getValue();
			}

			return data;
		},
		setMode:function(modeName){
			switch (modeName|| 'preAudit'){
				case 'preAudit':
					this.find('.preAudit').show();
					this.$mode = 'preAudit';
					this.setTitle("创意预审核");
					break;
				case 'getAuditResult':
					this.$mode = 'getAuditResult';
					this.find('.preAudit').hide();
					this.setTitle("获取创意审核结果");
					break;
				case 'audit':
					this.$mode = 'audit';
					this.find('.preAudit').hide();
					this.setTitle("创意请求渠道审核");
					break;
			}
			return this;
		},
		reset: function(){
			var vm = this.vm.$;
			var mods = this.$;

			vm._id = null;
			vm.Name = '';
			vm.CreateUserName = '';

			mods.preInfo.setValue('');
			mods.preStatus.reset();
			mods.exchangeIds.reset();

			return this;
		},
		save: function(){
			var data = this.getData();
			if(this.validate(data)){
				pubjs.loading.show();
				var url = this.getConfig(this.$mode + "Url");
				pubjs.mc.send(url, data, this.onSave.bind(this));
			}
		},
		onSave: function(err, data){
			pubjs.loading.hide();
			if(err){
				pubjs.alert(err.message);
				return false;
			} else if(!data.success){
				pubjs.alert(data.message|| LANG("未知错误"));
				return false;
			}

			pubjs.alert(LANG('预审操作成功'));
			this.fire('auditSuccess');
			this.reset();
			this.hide();
		},
		setTitle: function(title){
			var layout = this.getLayout();
			layout.setTitle(title);
			return this;
		},
		//响应点击完成事件
		onDialogOk: function(ev){
			this.save();
		},
		validate: function(data){
			if(this.$mode == 'preAudit' && !data.preStatus ){
				pubjs.alert(LANG('请指定审核状态'));
				return false;
			}
			return true;
		}
	});
	exports.base = previewAndPreAudit;
});